<?php
/**
 * This file is part of the Jindai.
 * @copyright Copyright (c) 2019 All Rights Reserved.
 * @author jindai <jindai0305@gmail.com>
 */

namespace App\Http\Controllers\Api\v1\Admin;


use App\Http\Controllers\Api\v1\Controller;
use App\Http\Resources\Song;
use App\Http\Resources\SongCollection;
use App\Repositories\SongRepository;
use Illuminate\Http\Request;

/**
 * Class SongController
 * @package App\Http\Controllers\Api\v1
 *
 * @SWG\Tag(name="Admin - Song", description="后台歌曲")
 */
class SongController extends Controller
{
    /**
     * @inheritdoc
     */
    public function __construct(SongRepository $model)
    {
        parent::__construct();
        $this->_model = $model;
    }

    /**
     * 获取歌曲列表
     *
     * @param Request $request
     * @return array
     *
     * @SWG\Get(path="/admin/songs",
     *     tags={"Admin - Song"},
     *     description="获取歌曲列表",
     *     produces={"application/json"},
     *     security={{"api_key": {}}},
     *     @SWG\Parameter(in="query", name="q", type="string", description="搜索内容 按标题"),
     *     @SWG\Parameter(ref="#/parameters/offsetPageParam"),
     *     @SWG\Parameter(ref="#/parameters/offsetLimitParam"),
     *     @SWG\Response(response=200, description="success",
     *          @SWG\Schema(type="array",
     *              @SWG\Items(ref="#/definitions/songDefault"),
     *          )
     *     ),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401, description="未登录")
     * )
     */
    public function list(Request $request)
    {
        $list = $this->model->paginate($request);
        return $this->resourceInstance(SongCollection::class, $list)->addMeta($this->model->getMeta($request));
    }

    /**
     * 新增
     *
     * @param Request $request
     * @return mixed
     *
     * @SWG\Definition(
     *     definition="songBodyParams",
     *     type="object",
     *     @SWG\Property(property="name", type="string", description="歌名", example="一路向北 (电影《头文字Ｄ》插曲)"),
     *     @SWG\Property(property="song_id", type="string", description="歌曲ID", example="3215"),
     *     @SWG\Property(property="song_images", type="array", description="名称",
     *          @SWG\Items(type="object",
     *              @SWG\Property(property="img", type="string", description="歌曲封面", example="https://d.musicapp.migu.cn/prod/file-service/file-down/b1899d500dda5db2da11df3efc89cba6/ec5a5dd821c0d251c5acf5deb8998d5b/342257b1d79325a96aef418533bd6ebd"),
     *              @SWG\Property(property="imgSizeType", type="string", description="封面size", example="03"),
     *          )
     *     ),
     *     @SWG\Property(property="singer", type="string", description="名称", example="周杰伦"),
     *     @SWG\Property(property="singer_id", type="string", description="歌星ID", example="112"),
     *     @SWG\Property(property="tags", type="array", description="标签组",
     *          @SWG\Items(type="string", example="流行"),
     *     ),
     *     @SWG\Property(property="album_name", type="string", description="专辑名", example="十一月的萧邦"),
     *     @SWG\Property(property="album_id", type="string", description="专辑ID", example="8591"),
     *     @SWG\Property(property="album_images", type="array", description="名称",
     *          @SWG\Items(type="object",
     *              @SWG\Property(property="img", type="string", description="专辑封面", example="https://d.musicapp.migu.cn/prod/file-service/file-down/b1899d500dda5db2da11df3efc89cba6/ec5a5dd821c0d251c5acf5deb8998d5b/342257b1d79325a96aef418533bd6ebd"),
     *              @SWG\Property(property="imgSizeType", type="string", description="封面size", example="03"),
     *          )
     *     ),
     *     @SWG\Property(property="content_id", type="string", description="内容ID", example="600902000006889222"),
     *     @SWG\Property(property="copyright_id", type="string", description="版权ID", example="60054701958"),
     *     @SWG\Property(property="invalidate", type="string", description="过期时间", example="2022-11-30"),
     *     @SWG\Property(property="lrc_url", type="string", description="歌词链接", example="https://d.musicapp.migu.cn/prod/file-service/file-down/4eedd78464c21ce789dea6928415b323/337d3c18efc6843b056068d9688f5a35/36345a19a98cf3ebd56e98f6309ca051"),
     *     @SWG\Property(property="mrc_url", type="string", description="伴奏链接", example="https://d.musicapp.migu.cn/prod/file-service/file-down/4eedd78464c21ce789dea6928415b323/e69317e43017ae4bd13859efdbfc60e3/fb10c2e29bb5e0dd02ef6b53b27fecc9"),
     *     @SWG\Property(property="top_quality", type="string", description="默认音质", example="SQ"),
     *     @SWG\Property(property="top_url", type="string", description="默认链接", example="http://tyst.migu.cn/public/product11/2018/06/21/2009年06月26日博尔普斯/歌曲下载/MP3_320_16_Stero/一路向北-周杰伦.mp3"),
     *     @SWG\Property(property="rate_formats", type="array", description="名称",
     *          @SWG\Items(type="object",
     *              @SWG\Property(property="fileType", type="string", description="专辑封面", example=""),
     *              @SWG\Property(property="format", type="string", description="封面size", example="020010"),
     *              @SWG\Property(property="formatType", type="string", description="封面size", example="HQ"),
     *              @SWG\Property(property="price", type="string", description="封面size", example="200"),
     *              @SWG\Property(property="resourceType", type="string", description="封面size", example="2"),
     *              @SWG\Property(property="size", type="string", description="封面size", example="020010"),
     *              @SWG\Property(property="url", type="string", description="封面size", example="http://tyst.migu.cn/public/product11/2018/06/21/2009年06月26日博尔普斯/歌曲下载/MP3_320_16_Stero/一路向北-周杰伦.mp3"),
     *          )
     *     ),
     * )
     *
     * @SWG\Post(path="/admin/song",
     *     tags={"Admin - Song"},
     *     summary="新增歌曲",
     *     description="新增歌曲",
     *     security={{"api_key": {}}},
     *     deprecated=false,
     *     @SWG\Parameter(in="body", name="body", description="提交的数据", required=true,
     *         @SWG\Schema(ref="#/definitions/songBodyParams")
     *     ),
     *     @SWG\Response(response=200,description="success"),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401,description="未登录")
     * )
     */
    public function store(Request $request)
    {
        return $this->model->store($request);
    }

    /**
     * 修改
     *
     * @param $id
     * @param Request $request
     * @return mixed
     *
     * @SWG\Put(path="/admin/song/{id}",
     *     tags={"Admin - Song"},
     *     summary="修改歌曲",
     *     description="修改歌曲",
     *     security={{"api_key": {}}},
     *     deprecated=false,
     *     @SWG\Parameter(name="id",type="integer",in="path",description="资源id",required=true,default=1),
     *     @SWG\Parameter(in="body", name="body", description="提交的数据", required=true,
     *         @SWG\Schema(ref="#/definitions/songBodyParams")
     *     ),
     *     @SWG\Response(response=200,description="success"),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401,description="未登录")
     * )
     */
    public function update($id, Request $request)
    {
        return $this->model->update($id, $request);
    }

    /**
     * 删除
     *
     * @param $id
     * @return bool|mixed|null
     * @throws \Exception
     *
     * @SWG\Delete(path="/admin/song/{id}",
     *     tags={"Admin - Song"},
     *     summary="删除歌曲",
     *     description="删除歌曲",
     *     security={{"api_key": {}}},
     *     deprecated=false,
     *     @SWG\Parameter(name="id",type="integer",in="path",description="资源id",required=true,default=1),
     *     @SWG\Response(response=200,description="success"),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401,description="未登录")
     * )
     */
    public function destroy($id)
    {
        $this->model->destroy($id);
    }

    /**
     * 详情
     *
     * @param $id
     * @return object
     *
     * @SWG\Get(path="/admin/song/{id}",
     *     tags={"Admin - Song"},
     *     summary="获取歌曲详情",
     *     description="获取一个歌曲资源详情",
     *     security={{"api_key": {}}},
     *     deprecated=false,
     *     @SWG\Parameter(name="id",type="integer",in="path",description="资源id",required=true,default=1),
     *     @SWG\Response(response=200,description="success",
     *          @SWG\Schema(ref="#/definitions/songDefault")
     *     ),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401,description="未登录")
     * )
     */
    public function show($id)
    {
        return $this->resourceInstance(Song::class, $this->model->findModel($id));
    }

    /**
     * 上架
     *
     * @param $id
     *
     * @SWG\Put(path="/admin/song/{id}/online",
     *     tags={"Admin - Song"},
     *     summary="上架",
     *     description="上架",
     *     security={{"api_key": {}}},
     *     deprecated=false,
     *     @SWG\Parameter(name="id",type="integer",in="path",description="资源id",required=true,default=1),
     *     @SWG\Response(response=200,description="success"),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401,description="未登录")
     * )
     */
    public function online($id)
    {
        $model = $this->model->findModel($id);
        $model->online();
    }

    /**
     * 上架
     *
     * @param $id
     *
     * @SWG\Put(path="/admin/song/{id}/offline",
     *     tags={"Admin - Song"},
     *     summary="下架",
     *     description="下架",
     *     security={{"api_key": {}}},
     *     deprecated=false,
     *     @SWG\Parameter(name="id",type="integer",in="path",description="资源id",required=true,default=1),
     *     @SWG\Response(response=200,description="success"),
     *     @SWG\Response(response=400,description="failed"),
     *     @SWG\Response(response=401,description="未登录")
     * )
     */
    public function offline($id)
    {
        $model = $this->model->findModel($id);
        $model->offline();
    }
}
